// pages/signIn/signIn.js
const app = getApp()
const util = require('../../../../utils/util.js')
const lbbService = require('../../../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSigned: false,
    isModalShow: false,
    types: [{
      value: 2,
      name: '休息'
    }, {
      value: 3,
      name: '上班',
      checked: true
    }],
    onDuty: 3,
    desc: '',
    signRecords: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      avatarUrl: app.globalData.userInfo.avatarUrl,
      realName: app.globalData.userInfo.nickName,
      accountPhone: '',
      type:options.type,
      nowDate: util.getNowDate(),
      scrollHeight:app.getScrollHeight(160),
    })
    this.getAddress();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    let that = this
    that.personalModal = that.selectComponent("#personalModal")
    that.toast = that.selectComponent("#toast")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    that.getRecords()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.data.type='';
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },
  hideModal: function(){
    this.setData({
      isModalShow: false,
      resignShow: false
    })
  },
  stop:function(){
    return
  },
  /**
   * 删除打卡的某一个医院
   */
  removeSignHos: function(e){
    util.print('removeSignHos',e);
    let id = e.currentTarget.dataset.id;
    let lbmId = e.currentTarget.dataset.lbmId;
    let lbmName = e.currentTarget.dataset.lbmName;
    let that = this;
    
    that.personalModal.showModal('确定删除'+ lbmName +'吗', function(){
      lbbService.removeSignHos({
        punchCardId:id,
        lbmId:lbmId,
      },res => {
        that.getRecords();
      })
    });
  },
  /**
   * 查看打卡详情
   */
  toDetail:function(e){
    console.log('toDetail>>',e)
    let cardDate = e.currentTarget.dataset.date
    let uid = e.currentTarget.dataset.uid
    let type = e.currentTarget.dataset.type==1?1:2
    wx.navigateTo({
      url: '../clockDetail/clockDetail?cardDate=' + cardDate + '&uid=' + uid + '&type='+type,
    })
  },
  getNowDate: function() {
    let reg = /\//g
    let nowTime = new Date()
    let nowDate = util.formatTime(nowTime).substr(0, 10).replace(reg, '-')
    return nowDate
  },
  isSigned: function() {
    let that = this
    let nowDate = that.getNowDate()
    let signRecords = that.data.signRecords
    signRecords.forEach(item => {
      if (item.cardDate == nowDate) {
        that.setData({
          isSigned: true
        })
      }
    })
  },
  inputDesc: function(e) {
    this.setData({
      desc: e.detail.value
    })
  },
  typeChange: function(e) {
    let that = this
    console.log('e>>', e);
    let onDuty = e.detail.value;
    let types = that.data.types;
    types.forEach(item=>{
      if(item.value==onDuty){
        item.checked = true;
      }else {
        item.checked = false;
      }
    })
    that.setData({
      onDuty: onDuty,
      types: types
    })
  },
  showsignInModal: function() {
    let that = this;
    // that.setData({
    //   isModalShow: true
    // })
    wx.navigateTo({
      url: './selectReason/selectReason?onDuty='+1,
    })
  },
  hideModal: function() {
    this.setData({
      isModalShow: false
    })
  },
  
  showRestModal: function() {
    let that = this
    let modalText = '确定今天休息吗？'
    that.personalModal.showModal(modalText, that.restIn)
  },
  showResignModal: function(e) {
    console.log('e>>', e)
    let that = this
    that.setData({
      date: e.currentTarget.dataset.date,
      resignShow: true
    })
  },
  hideResignModal: function() {
    this.setData({
      resignShow: false
    })
  },
  /**
   * 补卡
   */
  reSignIn: function() {
    let that = this
    that.hideResignModal();
    if(that.data.onDuty==3){
      wx.navigateTo({
        url: './selectReason/selectReason?onDuty='+3 + '&date='+that.data.date,
      })
    }else {
      lbbService.reSignTypes({
        cardDate: that.data.date,
        memo: that.data.desc,
        locationAddress: that.data.address ? that.data.address : '无',
        onDuty: that.data.onDuty
      }, function (res) {
        if (res.result == 2) {
          that.toast.showToast(res.data, '', 2000)
        } else {
          wx.showToast({
            title: '等待审批'
          })
          setTimeout(function () {
            that.getRecords()
          }, 1500)
        }
      })
    }
  },
  /**
   * 打卡
   */
  signIn: function () {
    let that = this
    that.hideModal()
    lbbService.signTypes({
      memo: that.data.desc,
      locationAddress: that.data.address ? that.data.address:'无',
      onDuty: 1
    }, function (res) {
      wx.showToast({
        title: '打卡成功',
        duration: 1500
      })
      setTimeout(function () {
        that.getRecords()
      }, 1500)
    })
  },
  /**
   * 休息卡
   */
  restIn: function() {
    let that = this
    lbbService.signTypes({
      memo: '',
      locationAddress: '无',
      onDuty: 0
    }, function (res) {
      wx.showToast({
        title: '打卡成功',
        duration: 1500
      })
      setTimeout(function () {
        that.getRecords()
      }, 1500)
    })
  },
  /**
   * 获取打卡列表，最近15天
   */
  getRecords: function() {
    let that = this
    lbbService.getRecentCard({},function(res){
      that.setData({
        signRecords: res.data
      })
      that.isSigned()
    })
  },
  /**
   * 查看我的打卡记录
   */
  tosignRecord: function() {
    wx.navigateTo({
      url: '../clockRecords/clockRecords',
    })
  },
  
  getAddress: function() {
    let that = this
    wx.request({
      url: 'https://api.map.baidu.com/geocoder/v2/',
      method: 'GET',
      data: {
        location: app.globalData.header.lat + ',' + app.globalData.header.lng,
        output: 'json',
        coordtype: 'wgs84ll',
        pois: 1,
        s: 1,
        latest_admin: 1,
        ak: 'GQKwKHdtu8CgmxXIkXdyTkdtlfvML4Gg'
      },
      success(res) {
        console.log('>>', res)
        if (res.data.status == 0) {
          that.data.address = res.data.result.formatted_address;
        }
      },
    })
  }
})